import { getRoleListApi, getUpdateRoleListApi, getPermissionListApi,addRoleListApi, updateRoleListApi, deleteRoleListApi } from '@/api/roleManage'

const roleManage  = {
  namespaced: true,
  state: {
    roleList: [],
    permissionList: [],
    currentConfig:{
      current : 1,
      pageSize: 10,
      total: 50,
      pageSizeOptions: ['10', '30', '50', '100'],
      showQuickJumper: true,
      showSizeChanger: true,
      showTotal : (total)=>{
        let pageConfig = roleManage.state.currentConfig
        return `共  ${total}  条记录   第  ${pageConfig.current} / ${Math.ceil(pageConfig.total/pageConfig.pageSize)}  页 `
      }
    },
    orde: null,
    orderBy: null
  },
  mutations: {
    SET_ROLELIST: (state, list) => {
      state.roleList = list
    },
    SET_TOTAL: (state, total) => {
      state.currentConfig.total = total
    },
    SET_CURRENT: (state, current) => {
      state.currentConfig.current = current
    },
    SET_PAGE: (state, pages) => {
      state.currentConfig.pageSize = pages
    },
    SET_PERMISSIONLIST: (state, permission) => {
      state.permissionList = permission
    },
    SET_ORDER: (state, {order, orderBy}) => {
      state.order = order
      state.orderBy = orderBy
    }
  },
  actions: {
    // 获取角色列表
    getAllRoleList ({ state, commit }) {
      const pageSize = state.currentConfig.pageSize,
            page = state.currentConfig.current,
            order = state.order,
            orderBy = state.orderBy
      return new Promise((resolve, reject) => {
        let msg = null
        getRoleListApi({ pageSize, page, order, orderBy })
          .then(res => {
            if (res.code == 0) {
              msg = res
              const { data } = res
              commit('SET_ROLELIST', data.data)
              commit('SET_TOTAL', data.total)
              commit('SET_CURRENT', data.current)
              commit('SET_PAGE', data.size)
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 获取权限列表
    getPermissionList ( {commit} ) {
      return new Promise((resolve, reject) => {
        getPermissionListApi()
          .then(res => {
            if (res.code == 0) {
              commit('SET_PERMISSIONLIST',res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 新增角色
    addRoleList ({commit} ,form) {
      return new Promise((resolve, reject) => {
        addRoleListApi(form)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 修改角色
    updateRole ({commit} ,form) {
      return new Promise((resolve, reject) => {
        updateRoleListApi(form)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 删除角色
    deleteRoleList ({commit} ,id) {
      return new Promise((resolve, reject) => {
        deleteRoleListApi({id})
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 获取当前修改的数据
    getUpdateList ({ commit }, id) {
      return new Promise((resolve, reject) => {
        getUpdateRoleListApi({id})
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    }
  },
  getters: {

  }
}

export default roleManage